<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\index\library\wechat\MiniApp;
use fast\Http;

class Juliang2 extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        $id = input('id'); // 商品id
        $appid = input('appid');
        $h5path = input('h5path');

        $path = input('path');
        $event_type = input('event_type'); // 回调id
        $clickid = input('clickid'); // 回调id

        $query = [];
        if ($id){
            $query['id'] = $id;
        }
        if ($h5path){
            $query['h5path'] = $h5path;
        }
        if ($clickid){
            $query['clickid'] = $clickid;
        }
        if ($event_type){
            $query['event_type'] = $event_type;
        }

        if (!$appid){
            $result = $this->getDomain($path,$query);

            if (!isset($result['url_link'])){
                $key = 'wanlshop_acctocken';
                Cache::store('redis')->rm($key);
                $result = $this->getDomain($path,$query);
            }
        }else{
            $result = $this->getOtherDomain($path,$query,$appid);

            if (!isset($result['url_link'])){
                $key = 'acctocken'.$appid;
                Cache::store('redis')->rm($key);
                $result = $this->getOtherDomain($path,$query,$appid);
            }
        }

//        $this->redirect($result['url_link']);
        var_dump($result['url_link']??'');
    }

    // 获取小程序跳转域名
    public function getDomain($path,$query = [])
    {
        $config = get_addon_config('wanlshop');
        $appid = $config['mp_weixin']['appid'];
        $secret = $config['mp_weixin']['appsecret'];

        $token = MiniApp::getAccessToken($appid,$secret);
        $query = http_build_query($query);

        $data = [
            "path" => $path,
            "query" => $query,
            "expire_type" => 1,
            "expire_interval" => 1,
        ];

        $options = [
            CURLOPT_HTTPHEADER => array('Content-type: application/json')
        ];

        $result = Http::post("https://api.weixin.qq.com/wxa/generate_urllink?access_token=$token",json_encode($data),$options);

        return json_decode($result,true);

    }

    public function getOtherDomain($path,$query = [],$q_appid)
    {
        $config = get_addon_config('wanlshop');
        $appid = $config['mp_weixin']['appid'];
        $secret = $config['mp_weixin']['appsecret'];

        if ($q_appid && $q_appid != $appid){
            $AppData = MiniApp::getAppInfo($q_appid);
            if ($AppData){
                $appid = $AppData['appid'];
                $secret = $AppData['secret_key'];
            }
        }


        $token = MiniApp::getOtherAccessToken($appid,$secret,'acctocken'.$appid);
        $query = http_build_query($query);

        $data = [
            "path" => $path,
            "query" => $query,
            "expire_type" => 1,
            "expire_interval" => 1,
        ];

        $options = [
            CURLOPT_HTTPHEADER => array('Content-type: application/json')
        ];

        $result = Http::post("https://api.weixin.qq.com/wxa/generate_urllink?access_token=$token",json_encode($data),$options);

        return json_decode($result,true);

    }

}